package hit.edu.zjc.UserInterface;

import java.util.ArrayList;
import java.util.Calendar;


public class UDDateType extends UserDef {

	public static final int LATESTDATE=101;//最晚的日期
	public static final int EARLIESTDATE=102;
	public static final int AVEDATE=103;//平均日期
	public static final int MOSTFREQUENTDATE=104;
	public static final int LEASTFREQUENTDATE=105;
	
	protected Calendar EarlyCalendar=null;
	protected boolean testEarly;
	protected Calendar LateCalendar=null;
	protected boolean testLate;
	protected Calendar correctCalendar=null;

	//EasyFill
	public UDDateType(boolean ic, boolean in,boolean tlate,Calendar latec,boolean tearly,Calendar earlyc,Calendar correctC) {
		super(DateType, ic, in,true);
		testEarly=tearly;
		testLate=tlate;
		EarlyCalendar=earlyc;
		LateCalendar=latec;
		correctCalendar=correctC;
	}
	//EasyFill 不一致修复
	public UDDateType(boolean ic, boolean in,boolean tlate,Calendar latec,boolean tearly,Calendar earlyc,Calendar correctC,int ft) {
		super(DateType, ic, in,true);
		testEarly=tearly;
		testLate=tlate;
		EarlyCalendar=earlyc;
		LateCalendar=latec;
		correctCalendar=correctC;
		FillType=ft;
	}
	//不处理
	public UDDateType()
	{
		super(DateType,false,true,true);
	}
	//值缺失填充
	public UDDateType(boolean tlate,Calendar latec,boolean tearly,Calendar earlyc,int ft,int[] cid)
	{
		super(DateType,true,false,false);
		FillType=ft;
		testLate=tlate;
		testEarly=tearly;
		EarlyCalendar=earlyc;
		LateCalendar=latec;
		RelevantColumID=new int[cid.length];
		for(int i=0;i<cid.length;i++)
		{
			RelevantColumID[i]=cid[i];
		}
		
	}
	@Override
	public boolean checkdata(String ob) {
		String data_str=ob;//yyyy-mm-dd
		
		if(data_str==null||data_str.trim().equals("")||data_str.trim().toLowerCase().equals("null"))
		{
			if(isNull)
				return true;
			else
				return false;
		}
		try{
			int year=Integer.parseInt(data_str.substring(0, 4));
			int month=Integer.parseInt(data_str.substring(5, 7));
			int date=Integer.parseInt(data_str.substring(8,10));
			Calendar checkdate=Calendar.getInstance();
			checkdate.set(year, month-1, date);
			if(testLate)//测试最晚日期。
			{
				//boolean before=checkdate.before(LateCalendar);比较是<而不是<=
				//if(!before)
					//return false;
				int before=checkdate.compareTo(LateCalendar);
				if(before>0)
					return false;
			}
			if(testEarly)//测试最早日期
			{
				//boolean after=checkdate.after(EarlyCalendar);
				//if(!after)
					//return false;
				int after=checkdate.compareTo(EarlyCalendar);
				if(after<0)
					return false;
			}
			return true;
		}
		catch(Exception e)
		{
			return false;
		}		
		
	}

	public String CalendarToString(Calendar c)
	{
		int year,month,date;
		year=c.getTime().getYear()+1900;
		month=c.getTime().getMonth()+1;
		date=c.getTime().getDate();
		String res=""+year+"-";
		if(month<=9)
			res=res+"0"+month+"-";//补一位。
		else
			res=res+month+"-";
		
		if(date<=9)
			res=res+"0"+date;//补一位
		else
			res=res+date;
		return res;
	}
	
	@Override
	public String correctdata() {
		if(isEasyFill)
		{
			return CalendarToString(correctCalendar);	
		}
		else
		{
			return null;
		}
	}
	@Override
	public String complexFill(String[] data)
	{
		if(FillType==EASYFILL)
			return CalendarToString(correctCalendar);	
		if(data.length==0)
			return null;
		ArrayList<String>strlist=new ArrayList<String>(data.length);
		for(int i=0;i<data.length;i++)
		{
			if(data[i]==null||data[i].trim().equals(""))
				continue;
			strlist.add(data[i]);
		}
		if(strlist.size()==0)
			return null;
		Calendar[] calendar_array=new Calendar[strlist.size()];
		for(int i=0;i<calendar_array.length;i++)
		{
			String data_str=strlist.get(i);//yyyy-mm-dd
			int year=Integer.parseInt(data_str.substring(0, 4));
			int month=Integer.parseInt(data_str.substring(5, 7));
			int date=Integer.parseInt(data_str.substring(8,10));
			Calendar checkdate=Calendar.getInstance();
			checkdate.set(year, month-1, date);
			calendar_array[i]=checkdate;
		}
		if(FillType==LATESTDATE)
		{
			int latestindex=0;
			for(int i=1;i<calendar_array.length;i++)
			{
				if(calendar_array[i].compareTo(calendar_array[latestindex])>0)
					latestindex=i;
			}
			return CalendarToString(calendar_array[latestindex]);
		}
		else if(FillType==EARLIESTDATE)
		{
			int earliestindex=0;
			for(int i=1;i<calendar_array.length;i++)
			{
				if(calendar_array[i].compareTo(calendar_array[earliestindex])<0)
					earliestindex=i;
			}
			return CalendarToString(calendar_array[earliestindex]);
		}
		else if(FillType==AVEDATE)
		{
			long sum=0;
			for(int i=0;i<calendar_array.length;i++)
			{
				sum=sum+calendar_array[i].getTimeInMillis();
			}
			sum=sum/calendar_array.length;//求平均值。
			Calendar c=Calendar.getInstance();
			c.setTimeInMillis(sum);
			return CalendarToString(c);
		}
		else if(FillType==MOSTFREQUENTDATE)
		{
			ArrayList<Calendar> calendarlist=new ArrayList<Calendar>();
			int[] times=new int [calendar_array.length];
			for(int i=0;i<calendar_array.length;i++)
			{
				Calendar c=calendar_array[i];
				int k=0;
				for(;k<calendarlist.size();k++)
				{
					if(calendarlist.get(k).compareTo(c)==0)//同一日期
					{
						times[k]++;
						break;
					}
				}
				if(k==calendarlist.size())
				{
					int size=calendarlist.size();
					calendarlist.add(size, c);
					times[size]=1;
				}
			}
			int mostindex=0;
			for(int i=1;i<calendarlist.size();i++)
			{
				if(times[i]>times[mostindex])
					mostindex=i;
			}
			return CalendarToString(calendarlist.get(mostindex));
		}
		else if(FillType==LEASTFREQUENTDATE)
		{
			ArrayList<Calendar> calendarlist=new ArrayList<Calendar>();
			int[] times=new int [calendar_array.length];
			for(int i=0;i<calendar_array.length;i++)
			{
				Calendar c=calendar_array[i];
				int k=0;
				for(;k<calendarlist.size();k++)
				{
					if(calendarlist.get(k).compareTo(c)==0)//同一日期
					{
						times[k]++;
						break;
					}
				}
				if(k==calendarlist.size())
				{
					int size=calendarlist.size();
					calendarlist.add(size, c);
					times[size]=1;
				}
			}
			int leastindex=0;
			for(int i=1;i<calendarlist.size();i++)
			{
				if(times[i]<times[leastindex])
					leastindex=i;
			}
			return CalendarToString(calendarlist.get(leastindex));
		}
		else
			return null;
		
	}

}
